package org.example.Soution001;

import java.util.Stack;

public class bstOrNot {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(32);
        root.left = new TreeNode(26);
        root.right = new TreeNode(47);
        root.left.left = new TreeNode(19);
        root.left.left.right = new TreeNode(27);
        root.right.right = new TreeNode(56);
        bstOrNot obj=new bstOrNot();
        System.out.println(obj.isValidBST(root));
    }

//    //不管怎么说，就用栈来解吧
//    Stack<TreeNode> stack=new Stack<TreeNode>();
//    //警惕边界，int的正好卡边了，所以用范围比它大一点的
//    double now= -Double.MAX_VALUE;
//    public boolean isValidBST(TreeNode root) {
//        while(!stack.isEmpty()||root!=null){
//            while(root!=null){
//                stack.push(root);
//                root=root.left;
//            }
//            root=stack.pop();
//            if(root.val<=now) return false;
//            now=root.val;
//            root=root.right;
//        }
//        return true;
//    }



    //另一种解法
    public boolean isValidBST(TreeNode root) {
        return isValidBST(root, Long.MIN_VALUE, Long.MAX_VALUE);
    }

    public boolean isValidBST(TreeNode root, long min, long max) {
        if(root==null) return true;

        if(root.val >= max || root.val <= min) return false;

        return isValidBST(root.left, min, root.val) && isValidBST(root.right, root.val, max);
    }
}
